const hbs = require('hbs');

const blocks = {};

/**
 * 覆盖对应 block 的模板扩展方法 extend
 * @method
 * created by wjxu on 2018-05-22T11:29:54+0800
 */
hbs.registerHelper('extend', (name, context) => {
  let block = blocks[name];
  if (!block) {
    blocks[name] = [];
    block = [];
  }

  block.push(context.fn(this)); // for older versions of handlebars, use block.push(context(this));
});

/**
 * 注册模板扩展方法 block
 * @method
 * created by wjxu on 2018-05-22T11:29:54+0800
 */
hbs.registerHelper('block', (name) => {
  const val = (blocks[name] || []).join('\n');

  // clear the block
  blocks[name] = [];
  return val;
});
